package com.example.springbootdemo.utils;

//import com.alibaba.excel.EasyExcelFactory;
//import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.metadata.Sheet;

import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description: springbootdemo
 * @Author: Anonymous
 * @Time: 2019-10-18 14:37
 */
public class ExcelExport {

//    @Autowired
//    LogUtil logUtil;
//
//    private static Sheet initSheet;
//
//    static {
//        initSheet = new Sheet(1, 0);
//        initSheet.setSheetName("sheet");
//        //设置自适应宽度
//        initSheet.setAutoWidth(Boolean.TRUE);
//    }
//
//    /**
//     * 生成excle
//     *
//     * @param filePath 绝对路径, 如：/home/chenmingjian/Downloads/aaa.xlsx
//     * @param data     数据源
//     * @param head     表头
//     */
//    public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head) {
//        Sheet sheet = initSheet;
//
//        if (head != null) {
//            List<List<String>> list = new ArrayList<>();
//            head.forEach(h -> list.add(Collections.singletonList(h)));
//            sheet.setHead(list);
//        }
//
//        OutputStream outputStream = null;
//        ExcelWriter writer = null;
//        try {
//            outputStream = new FileOutputStream(filePath);
//            writer = EasyExcelFactory.getWriter(outputStream);
//            writer.write1(data, sheet);
//        } catch (FileNotFoundException e) {
//            LogUtil.info("找不到文件或文件路径错误, 文件：{}" + filePath);
//        } finally {
//            try {
//                if (writer != null) {
//                    writer.finish();
//                }
//
//                if (outputStream != null) {
//                    outputStream.close();
//                }
//
//            } catch (IOException e) {
//                LogUtil.info("excel文件导出失败, 失败原因：{}" + e);
//            }
//        }
//
//    }
//
//    /**
//     * 下载excel文件
//     *
//     * @param excelPath excel文件路径
//     * @param excelName excel文件名
//     * @param response
//     */
//    public static void downloadExcel(String excelPath, String excelName, HttpServletResponse response) {
//        try (InputStream fis = new BufferedInputStream(new FileInputStream(excelPath))) {
//            byte[] buffer;
//            buffer = new byte[fis.available()];
//            fis.read(buffer);
//            response.reset();
//            OutputStream os = new BufferedOutputStream(response.getOutputStream());
//            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8"));
//            response.setContentType("application/vnd.ms-excel;charset=utf-8");// 定义输出类型
//            os.write(buffer);
//            os.flush();
//            os.close();
//        } catch (Exception e) {
//            LogUtil.info("文件下载失败" + e);
//        } finally {
//            try {
//                File file = new File(excelPath);
//                file.delete();
//            } catch (Exception e) {
//                LogUtil.info("文件删除失败" + e);
//            }
//        }
//    }
}
